// Copyright (C) joyinter
// All rights reserved.
// 
// Author : Jiang Li
// Date   : 2004-6-24

#include "stdafx.h"
#include "pagectrl.h"
#include ".\pagectrl.h"

// class CJoyPageCtrl

IMPLEMENT_DYNAMIC(CJoyPageCtrl, CJoyWnd)

//##ModelId=44CF17B601F4
CJoyPageCtrl::CJoyPageCtrl(void)
{
    m_container = NULL;
    m_tabSet    = NULL;

    m_tabSpace  = 1;
    m_tabStart  = 0;

    m_tabGroup.m_autoCheck = true;
}

//##ModelId=44CF17B601F6
CJoyPageCtrl::~CJoyPageCtrl(void)
{
}

//##ModelId=44CF17B601D7
CJoyPageContainer* CJoyPageCtrl::CreateContainer()
{
    return new CJoyPageContainer();
}

//##ModelId=44CF17B601D9
CJoyPageTabSet* CJoyPageCtrl::CreateTabSet()
{
    return new CJoyPageTabSet();
}

//##ModelId=44CF17B601F9
void CJoyPageCtrl::AddPage(const CString& name, CJoyWnd* page)
{
    // must not be created.
    ASSERT(page->m_hWnd == NULL);

    CPageItem item;
    item.m_name = name;
    item.m_page = page;
    item.m_tab  = new CJoyCheckBox();
    item.m_tab->m_name = name;
    item.m_tab->Create(m_tabSet, CRect(0, 0, 10, 10));
	item.m_tab->SetCheckEvent(this, static_cast<CJoyCheckBox::PCheckEvent>(&CJoyPageCtrl::OnSelTab));

    if(page->m_name.IsEmpty())
    {
        page->m_name = name;
    }
    page->Create(m_container, CRect(0, 0, 10, 10));
    page->SetVisible(false);

    m_items.AddTail(item);
    m_tabGroup.Add(item.m_tab);

    BuildTabArea();
}

//##ModelId=44CF17B601CA
POSITION CJoyPageCtrl::GetItemPos(const CString& name)
{
    POSITION pos = m_items.GetHeadPosition();
    while(pos)
    {
        POSITION tempPos = pos;
        CPageItem& item = m_items.GetNext(pos);
        if(item.m_name == name)
        {
            return tempPos;
        }
    }
    return NULL;
}

//##ModelId=44CF17B60204
CJoyWnd* CJoyPageCtrl::RemovePage(const CString& name)
{
    POSITION pos = GetItemPos(name);
    if(pos != NULL)
    {
        CPageItem& item = m_items.GetAt(pos);

        CJoyWnd* page = item.m_page;

        item.m_tab->Destroy();
        delete item.m_tab;
        m_items.RemoveAt(pos);

        BuildTabArea();

        return page;
    }
    return NULL;
}

//##ModelId=44CF17B60206
void CJoyPageCtrl::ShowPage(const CString& name, bool show)
{
    POSITION pos = GetItemPos(name);
    if(pos != NULL)
    {
        CPageItem& item = m_items.GetAt(pos);

        //item.m_page->SetVisible(show);
        //if(show)
        //{
        //    return;
        //}
        if(item.m_tab->IsChecked())
        {
            POSITION tempPos = pos;
            m_items.GetNext(tempPos);
            if(tempPos == NULL)
            {
                tempPos = pos;
                m_items.GetPrev(tempPos);
            }
            if(tempPos != NULL)
            {
                m_items.GetAt(tempPos).m_tab->SetCheck(true);
            }
            else
            {
                item.m_tab->SetCheck(false);
            }
        }
        item.m_tab->SetVisible(show);
    }
}

//##ModelId=44CF17B60214
void CJoyPageCtrl::SelectPage(const CString& name)
{
    POSITION pos = GetItemPos(name);
    if(pos != NULL)
    {
        CPageItem& item = m_items.GetAt(pos);

        item.m_tab->SetCheck(true);
    }
}

//##ModelId=44CF17B601CD
void CJoyPageCtrl::BuildTabAreaLeft()
{
    CRect area = m_tabSet->GetArea();

    int tabStart = m_tabStart;
    if(m_tabStart < 0)
    {
        tabStart = area.Height()+m_tabStart+1;
    }
    POSITION pos = m_items.GetHeadPosition();
    while(pos)
    {
        CPageItem& item = m_items.GetNext(pos);
        CJoyCheckBox* tab = item.m_tab;
        int w = tab->GetArea().Width();
        int h = tab->GetArea().Height();
        int left, top;
        if(m_tabStart < 0)
        {
            tabStart    -= h;
            top         = tabStart;
            tabStart    -= m_tabSpace;
        }
        else
        {
            top         = tabStart;
            tabStart    += h;
            tabStart    += m_tabSpace;
        }
        left = area.Width()-w;
        tab->SetArea(CRect(left, top, left+w, top+h));
    }
}

//##ModelId=44CF17B601D4
void CJoyPageCtrl::BuildTabAreaTop()
{
    CRect area = m_tabSet->GetArea();

    int tabStart = m_tabStart;
    if(m_tabStart < 0)
    {
        tabStart = area.Width()+m_tabStart+1;
    }
    POSITION pos = m_items.GetHeadPosition();
    while(pos)
    {
        CPageItem& item = m_items.GetNext(pos);
        CJoyCheckBox* tab = item.m_tab;
        int w = tab->GetArea().Width();
        int h = tab->GetArea().Height();
        int left, top;
        if(m_tabStart < 0)
        {
            tabStart    -= w;
            left    = tabStart;
            tabStart    -= m_tabSpace;
        }
        else
        {
            left    = tabStart;
            tabStart    += w;
            tabStart    += m_tabSpace;
        }
        top = area.Height()-h;
        tab->SetArea(CRect(left, top, left+w, top+h));
    }
}

//##ModelId=44CF17B601D5
void CJoyPageCtrl::BuildTabAreaRight()
{
    CRect area = m_tabSet->GetArea();

    int tabStart = m_tabStart;
    if(m_tabStart < 0)
    {
        tabStart = area.Height()+m_tabStart+1;
    }
    POSITION pos = m_items.GetHeadPosition();
    while(pos)
    {
        CPageItem& item = m_items.GetNext(pos);
        CJoyCheckBox* tab = item.m_tab;
        int w = tab->GetArea().Width();
        int h = tab->GetArea().Height();
        int left, top;
        if(m_tabStart < 0)
        {
            tabStart    -= h;
            top         = tabStart;
            tabStart    -= m_tabSpace;
        }
        else
        {
            top         = tabStart;
            tabStart    += h;
            tabStart    += m_tabSpace;
        }
        left = 0;
        tab->SetArea(CRect(left, top, left+w, top+h));
    }
}

//##ModelId=44CF17B601D6
void CJoyPageCtrl::BuildTabAreaBottom()
{
    CRect area = m_tabSet->GetArea();

    int tabStart = m_tabStart;
    if(m_tabStart < 0)
    {
        tabStart = area.Width()+m_tabStart+1;
    }
    POSITION pos = m_items.GetHeadPosition();
    while(pos)
    {
        CPageItem& item = m_items.GetNext(pos);
        CJoyCheckBox* tab = item.m_tab;
        int w = tab->GetArea().Width();
        int h = tab->GetArea().Height();
        int left, top;
        if(m_tabStart < 0)
        {
            tabStart    -= w;
            left    = tabStart;
            tabStart    -= m_tabSpace;
        }
        else
        {
            left    = tabStart;
            tabStart    += w;
            tabStart    += m_tabSpace;
        }
        top = 0;
        tab->SetArea(CRect(left, top, left+w, top+h));
    }
}

//##ModelId=44CF17B601CC
void CJoyPageCtrl::BuildTabArea()
{
    switch(m_tabSet->GetAlign())
    {
    case AT_Left:
        BuildTabAreaLeft();
        break;
    case AT_Right:
        BuildTabAreaRight();
        break;
    case AT_Bottom:
        BuildTabAreaBottom();
        break;
    case AT_Top:
    default:
        BuildTabAreaTop();
        break;
    }
    if(m_container != NULL)
    {
        ((CJoyCtrl*)m_container)->InvalidateCtrl();
    }
}

BEGIN_MESSAGE_MAP(CJoyPageCtrl, CJoyWnd)
    ON_WM_CREATE()
END_MESSAGE_MAP()

//##ModelId=44CF17B601E8
void CJoyPageCtrl::OnSelTab(CJoyCheckBox* box, bool& checked)
{
    //if(!checked)
    //{
    //    return;
    //}
    POSITION pos = m_items.GetHeadPosition();
    while(pos)
    {
        CPageItem& item = m_items.GetNext(pos);
        if(item.m_tab == box)
        {
            //CJoyWnd* pWnd = (CJoyWnd*)m_container->GetWnd();
            //if(pWnd == item.m_page)
            //{
            //    return;
            //}
            //if(pWnd != NULL)
            //{
            //    pWnd->SetVisible(false);
            //}
            if(checked)
            {
                item.m_page->SetVisible(true);
                m_container->SetWnd(item.m_page);
            }
            else
            {
                item.m_page->SetVisible(false);
            }
        }
    }
    if(m_container != NULL)
    {
        ((CJoyCtrl*)m_container)->InvalidateCtrl();
    }
}

//##ModelId=44CF17B601E5
void CJoyPageCtrl::CM_LoadSkin(CJoyXmlElement* pSkinElem)
{
    __super::CM_LoadSkin(pSkinElem);

    m_tabSpace  = pSkinElem->ReadIntD("tab_space", m_tabSpace);
    m_tabStart  = (int)pSkinElem->ReadIntD("tab_start", m_tabStart);

}

//##ModelId=44CF17B60216
int CJoyPageCtrl::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
    if (CJoyWnd::OnCreate(lpCreateStruct) == -1)
        return -1;

    m_tabSet    = CreateTabSet();
    m_tabSet->m_pageCtrl = this;
    m_tabSet->Create(this, CRect(0, 0, 100, 100));

    m_container = CreateContainer();
    m_container->m_pageCtrl = this;
    m_container->Create(this, CRect(0, 0, 100, 100));

    return 0;
}


// class CJoyPageContainer

IMPLEMENT_DYNAMIC(CJoyPageContainer, CJoyWndContainer)

//##ModelId=44CF17B60177
CJoyPageContainer::CJoyPageContainer()
{
    m_pageCtrl  = NULL;
    m_name  = "page_container";
    m_align = AT_Client;
}

//##ModelId=44CF17B60178
CJoyPageContainer::~CJoyPageContainer(void)
{
}

BEGIN_MESSAGE_MAP(CJoyPageContainer, CJoyWndContainer)
END_MESSAGE_MAP()


// class CJoyPageTabSet

IMPLEMENT_DYNAMIC(CJoyPageTabSet, CJoyWnd)

//##ModelId=44CF17B6018B
CJoyPageTabSet::CJoyPageTabSet()
{
    m_pageCtrl  = NULL;
    m_name = "tab_set";
    m_align = AT_Top;

    m_skinArea  = true;
    m_skinAlign = true;
}

//##ModelId=44CF17B6018C
CJoyPageTabSet::~CJoyPageTabSet(void)
{
}

BEGIN_MESSAGE_MAP(CJoyPageTabSet, CJoyWnd)
    ON_WM_SIZE()
END_MESSAGE_MAP()

//##ModelId=44CF17B6018F
void CJoyPageTabSet::OnSize(UINT nType, int cx, int cy)
{
    CJoyWnd::OnSize(nType, cx, cy);

    m_pageCtrl->BuildTabArea();
}
